def trapz(y, x=None, dx=1.0):
    """ Integrate using the trapezoidal rule.
    
    The number of trapeziums used is one less than the number of y values. 
    
    y: sequence of length N 
      The function values y = f(x).
    x: sequence of length N, optional
      The x values corresponding to the y values. If None (the default), the 
      width of the trapeziums is assumed to be given by dx.
    dx: float, optional
      The width of the trapeziums. This is only used if x is None. The default 
      value is 1.0.
    
    Examples:

    >>> from math import sin,pi
    >>> xvals = [i * pi / 10 for i in range(11)]
    >>> yvals = [sin(x) for x in xvals]
    >>> print trapz(yvals, x=xvals)
    1.98352353751
    """

    # Pure python (no numpy)
    if x is None:
        diff = [dx] * (len(y) - 1)
    else:
        diff = [x1 - x0 for x0,x1 in zip(x[:-1], x[1:])]
    
    return sum(d * (y0 + y1) / 2.0 for d,y0,y1 in zip(diff, y[:-1], y[1:]))
